﻿@model WXY.Course.Model.UserInfo
<div class="m_rtop">
    <h3>
        <label>
            个人信息
        </label>
        <span>></span>个人详细
    </h3>
</div>
<div>
    <table style="background: transparent; margin: 20px 0;" id="t_passwordchange">
        <tbody>
            <tr>
                <td>
                    <label>用户名:</label>
                </td>
                <td>
                    <input type="text" maxlength="14" name="name" id="name" value="@Model.Name" />
                </td>
                <td class="need">*</td>
            </tr>
            <tr>
                <td>
                    <label>原始密码:</label>
                </td>
                <td>
                    <input type="password" maxlength="14" name="oldPassword" id="oldPassword" value="@Model.Password"/>
                </td>
                <td class="need">*</td>
            </tr>
            <tr>
                <td>
                    <label>新密码:</label>
                </td>
                <td>
                    <input type="password" maxlength="14" name="password" id="password">
                </td>
                <td class="need">*</td>
            </tr>
            <tr>
                <td>
                    <label>确认密码:</label>
                </td>
                <td>
                    <input type="password" maxlength="14" name="password2" id="password2">
                </td>
                <td class="need">*</td>
            </tr>
            <tr>
                <td>
                    <label>真实姓名:</label>
                </td>
                <td>
                    <input type="text" maxlength="14" name="realName" id="realName">
                </td>
                <td class="need">*</td>
            </tr>
            <tr>
                <td>
                    <label>用户级别:</label>
                </td>
                <td>
                    <select id="level" name="level">
                        <option value="0">普通用户</option>
                        <option value="1">管理员</option>
                    </select>
                </td>
                <td class="need">*</td>
            </tr>
            <tr>
                <td colspan="3">
                    <div style="color: black; margin-top: 20px; text-align: center;">
                        <input type="button" id="btnUpdate" value="更新密码" />
                        <input type="reset" value="重置" />
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
</div>
<script type="text/javascript">
    require(['jquery', 'standardArtDialog'], function ($, standardArtDialog) {
        var generateData = function () {
            return {
                oldPassword: $("#oldPassword").val(),
                password: $("#password").val(),
                password2: $("#password2").val()
            };
        }
        var normalDialog = function (msg, fn) {
            standardArtDialog.dialog({
                drag: true,
                lock: true,
                width: 310,
                height: 50,
                title: '提示',
                content: msg,
                ok: function () {
                    if (fn)
                        fn();
                    else
                        this.close();
                }
            });
        };
        var valid = function (data) {
            var errorMessage = '';
            var success = true;
            if (data.oldPassword.length == 0) {
                errorMessage += '*旧密码不能为空!<br/>';
                success = false;
            }
            if (data.password.length == 0) {
                errorMessage += '*密码不能为空!<br/>';
                success = false;
            }
            else if (!/^([A-Za-z]|[1-9]){6,14}$/.test(data.password)) {
                errorMessage += '*密码格式不正确,应为6-14位字母或数字组合!<br/>';
                success = false;
            }
            if (data.password2.length == 0) {
                errorMessage += '*确认密码不能为空!<br/>';
                success = false;
            }
            else if (data.password !== data.password2) {
                errorMessage += '*确认密码与密码不一致!<br/>';
                success = false;
            }
            else if (!/^[^\s]{6,14}$/.test(data.password2)) {
                errorMessage += '*确认密码格式不正确,应为4-16位字符组合!<br/>';
                success = false;
            }
            if (!success)
                normalDialog(errorMessage);
            return success;
        };

        $("#btnUpdate").on("click", function () {
            var postData = generateData();
            if (valid(postData)) {
                $.ajax({
                    type: "post",
                    url: "@Url.Action("UpdatePass", "Account")",
                    data: postData,
                    success: function (data) {
                        if (data.IsSuccess) {
                            normalDialog("更改成功", function () {
                                location.href = "@Url.Action("UserDetail")";
                            });
                            return;
                        }
                        normalDialog(data.Message);
                    }
                })
            }
        })
    });
</script>
